<template>
  <div class="follow-up-records">
    <div class="follow-up-records__items">
      <div class="radio-item">
        <div class="title">跟进记录</div>
        <el-radio-group v-model="filterListData[0].value[0]">
          <el-radio :value="item.value" v-for="item in filterListData[0].option.data">{{ item.label }}</el-radio>
        </el-radio-group>
      </div>
      <div>
        <div class="title">最后一次跟进时间</div>

        <div class="date-change">
          <el-select v-model="filterListData[1].compare" :teleported="false" @change="changeDateType">
            <el-option
              v-for="item in filterListData[1].condition"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-date-picker
            v-if="filterListData[1].compare === 'between'"
            v-model="filterListData[1].value"
            :teleported="false"
            type="daterange"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            date-format="YYYY-MM-DD"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <el-date-picker
            v-else
            v-model="filterListData[1].value[0]"
            :teleported="false"
            type="date"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            date-format="YYYY-MM-DD"
          />
        </div>
      </div>
    </div>
    <div class="follow-up-records__buttons">
      <el-button type="primary" class="buttons-cancel" link size="default" @click="handelReset">重置</el-button>
      <el-button type="primary" class="buttons-confirm" size="default" @click="handelConfirm">确定</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from "vue";
import { cloneDeep } from "lodash";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();

const props = defineProps({
  filterList: {
    type: Array,
    default: () => [
      {
        title: "所有记录",
        filterId: "follow_user_id",
        condition: [
          {
            label: "等于",
            value: "eq"
          }
        ],
        option: {
          type: "InputSelect",
          lazy: false,
          dataSource: {},
          data: [
            {
              label: "所有记录",
              value: "all"
            },
            {
              label: "我添加的记录",
              value: "my"
            }
          ]
        },
        compare: "eq",
        value: [""]
      },
      {
        title: "最后一次跟进时间",
        filterId: "follow_last_date",
        condition: [
          {
            label: "时间范围",
            value: "between"
          },
          {
            label: "大于某日",
            value: "gt"
          },
          {
            label: "等于某日",
            value: "eq"
          },
          {
            label: "等于某日",
            value: "lt"
          }
        ],
        option: {
          type: "SelectInput",
          lazy: false,
          dataSource: {},
          data: []
        },
        compare: "between",
        value: []
      }
    ]
  }
});
const emit = defineEmits(["handelFilterData"]);
const filterListData = ref([]);

const optionMap = new Map([
  ["all", ""],
  ["my", userStore.userInfo.userId]
]);

watch(
  () => props.filterList,
  newVal => {
    const newValData = cloneDeep(newVal);
    filterListData.value = [...newValData];
    filterListData.value[0].value[0] = newValData[0].value[0] === "" ? "all" : "my";
  },
  { deep: true, immediate: true }
);

const handelReset = () => {
  filterListData.value[0].value = [""];
  filterListData.value[1].compare = "between";
  filterListData.value[1].value = [];
  emit("handelFilterData", filterListData.value);
};
const handelConfirm = () => {
  const cloneData = cloneDeep(filterListData.value);
  cloneData[0].value[0] = optionMap.get(filterListData.value[0].value[0]);
  emit("handelFilterData", cloneData);
};

/**
 * 时间类型
 */
const changeDateType = e => {
  if (e === "between") {
    return (filterListData.value[1].value = []);
  }
  filterListData.value[1].value = [""];
};
</script>

<style scoped lang="scss">
@import "followUpRecords";
</style>
